<?php

/**
 * This is the model class for table "Entry".
 *
 * The followings are the available columns in table 'Entry':
 * @property string $id
 * @property string $name
 * @property integer $votes
 * @property integer $status
 * @property string $imageId
 * @property string $contestId
 * @property string $creatorId
 * @property string $created
 * @property string $updated
 * @property integer $deleted
 *
 * The following are the available relations in the table 'Entry':
 * @property Contest $contest
 * @property User $submitter
 * @property Image $image
 * @property integer $voteCount
 */
class Entry extends CActiveRecord
{
	const STATUS_UNAPPROVED = 0;
	const STATUS_APPROVED = 1;

	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className the class name
	 * @return Entry the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'Entry';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		return array(
			array('name, contestId', 'required'),
			array('status, deleted', 'numerical', 'integerOnly'=>true),
			array('name', 'length', 'max'=>255),
			array('status, contestId, imageId, creatorId', 'length', 'max'=>10),
			array('votes, status, imageId, creatorId, created, updated', 'safe'),
			array('id, name, votes, contestId, imageId, creatorId, created, updated', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		return array(
			'contest' => array(self::BELONGS_TO, 'Contest', 'contestId'),
			'submitter' => array(self::BELONGS_TO, 'User', 'creatorId'),
			'image' => array(self::BELONGS_TO, 'Image', 'imageId'),
			'voteCount' => array(self::STAT, 'Vote', 'entryId'),
		);
	}

	/**
	 * @return array attached behaviors.
	 */
	public function behaviors()
	{
		return array(
			'ext.auditbehavior.AuditBehavior',
			'ext.deletedbehavior.DeletedBehavior',
			'ImgRecordBehavior',
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => Yii::t('label','Id'),
			'name' => Yii::t('label','Name'),
			'votes' => Yii::t('label','Votes'),
			'status' => Yii::t('label','Status'),
			'imageId' => Yii::t('label','Image'),
			'contestId' => Yii::t('label','Contest'),
			'creatorId' => Yii::t('label','Submitter'),
			'created' => Yii::t('label','Created'),
			'updated' => Yii::t('label','Updated'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		$criteria=new CDbCriteria;

		$criteria->with[] = 'contest';
		$criteria->with[] = 'submitter';

		$criteria->compare('t.id',$this->id,true);
		$criteria->compare('t.name',$this->name,true);
		$criteria->compare('t.votes',$this->votes,true);
		$criteria->compare('t.status',$this->status,true);
		$criteria->compare('t.imageId',$this->imageId,true);
		$criteria->compare('contest.name',$this->contestId,true);
		$criteria->compare('submitter.name',$this->creatorId,true);
		$criteria->compare('t.created',$this->created,true);
		$criteria->compare('t.updated',$this->updated,true);

		$criteria->addCondition('t.deleted=0');

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * @return integer the id for the previous entry in the contest.
	 */
	public function getPrevId()
	{
		$command = Yii::app()->db->createCommand();
		$command->select('MAX(id) AS prevId');
		$command->from($this->tableName());
		$command->where('id<:id AND contestId=:contestId AND deleted=0', array(':id'=>$this->id,':contestId'=>$this->contestId));
		$result = $command->queryRow();
		return $result !== false ? $result['prevId'] : null;
	}

	/**
	 * @return integer the id for the next entry in the contest.
	 */
	public function getNextId()
	{
		$command = Yii::app()->db->createCommand();
		$command->select('MIN(id) AS nextId');
		$command->from($this->tableName());
		$command->where('id>:id AND contestId=:contestId AND deleted=0', array(':id'=>$this->id,':contestId'=>$this->contestId));
		$result = $command->queryRow();
		return $result !== false ? $result['nextId'] : null;
	}

	/**
	 * @return string the votes string.
	 */
	public function getVotesString()
	{
		return $this->votes > 0 ? '+ '.$this->votes : '0';
	}

	/**
	 * Returns whether a specific user has voted on this entry.
	 * @param $userId the user ID
	 * @return boolean
	 */
	public function hasVoted($userId)
	{
		$vote = Vote::model()->findByAttributes(array(
			'voterId'=>$userId,
			'entryId'=>$this->id,
			'contestId'=>$this->contestId,
		));

		return $vote !== null;
	}

	/**
	 * Returns the URL for this entry.
	 * @param array $params additional GET parameters (name=>value).
	 * @return string the URL to this entry.
	 */
	public function getUrl($params=array())
	{
		return Yii::app()->createUrl('entry/view', CMap::mergeArray($params,
				array('id'=>$this->id, 'name'=>$this->name)));
	}

	public function getStatusString()
	{
		$options = self::getStatusOptions();
		return isset($options[$this->status]) ? $options[$this->status] : Yii::t('admin','Unknown');
	}

	public static function getStatusOptions()
	{
		return array(
			self::STATUS_UNAPPROVED => Yii::t('admin','Unapproved'),
			self::STATUS_APPROVED => Yii::t('admin','Approved'),
		);
	}
}